<%
var hostSummary = {};
if (typeof processes != 'undefined') {
    for (var i = 0; i < processes.length; i++) {

        if (!hostSummary[processes[i].statename]) {
            hostSummary[processes[i].statename] = 1;
        } else {
            hostSummary[processes[i].statename]++;
        }
    }
}
sortedstatuses = [];
for (state in hostSummary) {
    sortedstatuses.push(state);
}
sortedstatuses.sort();
var summaryLabel = '';
for (state in sortedstatuses) {
    var stateColor = 'info';
    switch (sortedstatuses[state]) {
        case 'RUNNING': {
            stateColor = 'success';
        }
        break;
        case 'STOPPED': {
            stateColor = 'warning';
        }
        break;
        case 'EXITED': {
            stateColor = 'warning';
        }
        break;
        case 'FATAL': {
            stateColor = 'danger';
        }
        break;
    }
    summaryLabel += ' <span class="label label-' + stateColor + '">' + hostSummary[sortedstatuses[state]] + '</span>';
}
%>
<h4 >
    <div class="row">
        <div class="span2"><%= host.Name %></div>
        <div class="span2"><%= summaryLabel %></div>
        <div class="span2"><%= host.GroupName != null ? '<span class="label label-default">' + host.GroupName + '</span>' : '' %></div>
    </div>
</h4>
<% if (!error) { %>
    <button type="button" data-table="<%= host.idHost %>" class="btn pull-right table-collapse" style="margin-top: -47px;"><i class="glyphicon glyphicon-collapse-<%= hostCollapsed %>"></i></button>
    <%
        if (role == 'Admin') {
            %>
                <button type="button" data-host="<%= host.idHost %>" data-action="restartAll" class="btn btn-warning btn-labeled pull-right super-action" style="margin-top: -47px; margin-right: 65px;"><span class="btn-label"><i class="glyphicon glyphicon-repeat"></i></span> Restart all</button>
            <%
        }

        var hostDisplay = '';
        if (hostCollapsed == 'down') { hostDisplay = 'display: none;' }
    %>
    <table class="table table-hover table-striped" style="<%= hostDisplay %>" id="<%= host.idHost %>">
        <tr>
            <th style="width: 45%;">Process Name</th>
            <th style="width: 10%;">State</th>
            <th style="width: 20%;">Changed</th>
            <th style="width: 25%;">Actions</th>
        </tr>
        <% for (var i = 0; i < processes.length; i++){ %>
            <%
                var rowClass = 'info';
                switch (processes[i].statename) {
                    case 'RUNNING': {
                        rowClass = 'success';
                    }
                    break;
                    case 'STOPPED': {
                        rowClass = 'warning';
                    }
                    break;
                    case 'EXITED': {
                        rowClass = 'warning';
                    }
                    break;
                    case 'FATAL': {
                        rowClass = 'danger';
                    }
                    break;
                }
            %>
            <tr class="<%= rowClass %> process-row">
                <td>
                    <%= processes[i].name %>
                    <div class="spawn-error-div" style="display: none;">
                        <% if (processes[i].spawnerr != '') { %>
                            <hr>
                            <span style="font-weight: bold;">Error:</span><br>
                            <%= processes[i].spawnerr %>
                        <% } %>
                    </div>
                </td>
                <td>
                    <%= processes[i].statename %>
                </td>
                <td>
                    <%
                        var changeTime;
                        if (processes[i].stop != 0 && processes[i].stop > processes[i].start) {
                            changeTime = processes[i].stop;
                        } else {
                            changeTime = processes[i].start;
                        }
                    	  if (changeTime) {
                    %>
                    		    <%= moment.unix(changeTime).from(moment.unix(processes[i].now)) %>
					          <% } else { %>
							              Not started
          					<% } %>
                </td>
                <td>
                    <%
                        if (role == 'Admin') {
                            if (processes[i].group){
                              processFullname = processes[i].group + ':' + processes[i].name;
                            } else {
                              processFullname = processes[i].name;
                            }
                            if (processes[i].statename == 'RUNNING') {
                                %>
                                    <button type="button" class="btn btn-labeled btn-danger super-action" data-action="stop" data-process="<%= processFullname %>" data-host="<%= host.idHost %>"><span class="btn-label"><i class="glyphicon glyphicon-minus-sign"></i></span>Stop</button>
                                    <button type="button" class="btn btn-labeled btn-warning super-action" data-action="restart" data-process="<%= processFullname %>" data-host="<%= host.idHost %>"><span class="btn-label"><i class="glyphicon glyphicon-repeat"></i></span>Restart</button>
                                <%
                            } else if ((processes[i].statename == 'FATAL') || (processes[i].statename == 'STOPPED') || (processes[i].statename == 'EXITED')) {
                                %>
                                    <button type="button" class="btn btn-labeled btn-success super-action" data-action="start" data-process="<%= processFullname %>" data-host="<%= host.idHost %>"><span class="btn-label"><i class="glyphicon glyphicon-ok"></i></span>Start</button>
                                <%
                            } else {
                                %>
                                    <button type="button" class="btn btn-labeled btn-info"><span class="btn-label"><i class="glyphicon glyphicon-refresh"></i></span>Updating</button>
                                <%
                            }
                        }
                    %>
                    <a href="/log/<%= host.idHost %>/<%= processFullname %>/out" class="btn btn-default"><i class="glyphicon glyphicon-tasks"></i></a>
                </td>
            </tr>
        <% } %>
    </table>
<% } else { %>
    <div class="alert alert-danger">
        <strong>Error!</strong> <%= error %>
    </div>
<% } %>
<hr>
